package com.thermofisher.controller;

import com.thermofisher.beans.Customer;
import com.thermofisher.service.CustomerService;
import com.thermofisher.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author py
 * @since 2020-01-13
 */
@RestController
@RequestMapping("/customer")
public class CustomerController
{
    @Autowired
    private CustomerService customerService;

    @PostMapping("importExcel")
    public String importExcel(@RequestParam(value = "file") MultipartFile file)
    {
        //解析excel，
        List<Customer> customerList =
                FileUtil.importExcel(file, 1, 1, Customer.class);
        //也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass)导入

        List<Customer> allCustomerNo = customerService.getAllCustomerNo();
        if (allCustomerNo.size() > 0)
        {
            List<Customer> needUpdateData = customerService
                    .needUpdateData(customerList, allCustomerNo);
            List<Customer> needInsertData = customerService
                    .needInsertData(customerList, allCustomerNo);

            if (needUpdateData.size() > 0)
            {
                customerService.updateCustomerData(needUpdateData);
            }
            if (needInsertData.size() > 0)
            {
                customerService.insertCustomerData(needInsertData);
            }

        }
        else
        {
            customerService.insertCustomerData(customerList);
        }

        return "upload file success";
    }
}

